<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  	<el-breadcrumb-item :to="{ path: '/user' }">首页</el-breadcrumb-item>
		  	<el-breadcrumb-item>编辑客户</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form ref="form" :model="formData" label-width="80px">
		  	<el-form-item label="客户账号">
		    	<el-input v-model="formData.username" readonly></el-input>
		  	</el-form-item>

		  	<el-form-item label="客户密码">
		    	<el-input v-model="formData.password"></el-input>
		  	</el-form-item>

		  	<el-form-item label="客户名称">
		    	<el-input v-model="formData.customer"></el-input>
		  	</el-form-item>
		  	
		  	<el-form-item label="客户代码">
		    	<el-input v-model="formData.customer_sn"></el-input>
		  	</el-form-item>
		  	
		  	<el-form-item label="性别">
		    	 <el-radio v-model="formData.sex" label='0'>男</el-radio>
  				 <el-radio v-model="formData.sex" label='1'>女</el-radio>
		  	</el-form-item>
		  	<el-form-item label="生日">
		    	 <el-date-picker
			      v-model="formData.birthday" 
			      type="datetime"
			      @change="getTime"
			      format="yyyy-MM-dd HH:mm:ss" 
			      placeholder="选择日期时间">
			    </el-date-picker>
		  	</el-form-item>
		  	
		  	<el-form-item label="邮箱">
		    	<el-input v-model="formData.email"></el-input>
		  	</el-form-item>
		  	
		  	<el-form-item label="地址">
		    	<el-input v-model="formData.address"></el-input>
		  	</el-form-item>
		  	
		  	<el-form-item>
		    	<el-button type="primary" @click="addUser">修改客户</el-button>
		    	<el-button>取消</el-button>
		  	</el-form-item>
		</el-form>
	</div>
</template>

<script type="text/javascript">
	export default{
		data(){
			return{
				formData:{
					username:"",
					password:"",
					nickname:"",
					address:"",
					customer:"",
					email:"",
					customer_sn:"",
					status:1,
					birthday:'',
					_token:"",
					id:"",
					sex:''
				}
			}
		},
		created(){
			this.formData._token = window.axios.defaults.headers.common['X-CSRF-TOKEN'];
			this.getUserInfo();
			this.formData.id = this.$route.params.id;
		},
		methods:{
			handleRemove(file, fileList) {
		        console.log(file, fileList);
		    },
		    handlePreview(file) {
		        console.log(file);
		   	},
		    handlePictureCardPreview(file) {
		        this.dialogImageUrl = file.url;
		        this.dialogVisible = true;
		    },
		    handleExceed(files, fileList) {
	        	this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
	      	},
		    beforeRemove(file, fileList) {
		        return this.$confirm(`确定移除 ${ file.name }？`);
		    },
		    addUser(){
		    	axios.post('/api/user/edit',this.formData)
		    		.then(result => {
		    			if(result.data.code == 200){
		    				this.$notify({
		    					type:"success",
		    					message:result.data.message
		    				})
		    			}else{
		    				this.$notify.error(result.data.message);
		    			}
		    		})
		    		.catch(error => {

		    		})
		    },
		    getUserInfo(){
		    	axios.get("/api/user/info/"+this.$route.params.id)
		    		.then(res => {
		    			var data = res.data.result[0];
		    			//formData
		    			if(res.data.code == 200){
		    				this.formData.username = data.username;
		    				this.formData.password = data.password;
		    				this.formData.nickname = data.nickname;
		    				this.formData.status = data.status == 1 ? true : false;
		    				this.formData.sex = data.sex == 1 ? '1' : '0';
		    				this.formData.customer = data.customer;
		    				this.formData.customer_sn = data.customer_sn;
		    				this.formData.email = data.email;
		    				this.formData.address = data.address;
		    				this.formData.birthday =this.timeFormat(data.birthday);
		    			}
		    		})
		    },
	        addTime(m){
	    	return m<10?'0'+m:m 
		    },
			timeFormat(shijianchuo)
			{
				//shijianchuo是整数，否则要parseInt转换
				var sjc =shijianchuo *1000;
				var time = new Date(sjc);
				var y = time.getFullYear();
				var m = time.getMonth()+1;
				var d = time.getDate();
				var h = time.getHours();
				var mm = time.getMinutes();
				var s = time.getSeconds();
				return y+'-'+this.addTime(m)+'-'+this.addTime(d)+' '+this.addTime(h)+':'+this.addTime(mm)+':'+this.addTime(s);
			},
		}
	}
</script>

<style type="text/css">
	input[type=file] {
	    display: none;
	}
</style>